package com.sanchunhui.psc.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import com.j256.ormlite.android.AndroidDatabaseConnection
import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseApplication
import com.sanchunhui.psc.data.dao.PSCOpenHelper
import com.sanchunhui.psc.data.protocol.UserInfo
import com.sanchunhui.psc.injection.component.DaggerUserComponent
import com.sanchunhui.psc.injection.module.UserModule
import com.sanchunhui.psc.presenter.LogoutPresenter
import com.sanchunhui.psc.presenter.view.LogoutView
import com.sanchunhui.ui.activity.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_setting.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.sql.Savepoint

/*
    设置界面
 */
class SettingActivity :  BaseMvpActivity<LogoutPresenter>(), LogoutView, View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)

        tv_updatePwd.setOnClickListener(this)
        btn_logout.setOnClickListener(this)
//        mUserProtocolTv.onClick {
//            toast("用户协议")
//        }
//        mFeedBackTv.onClick {
//            toast("反馈意见")
//        }
//        mAboutTv.onClick {
//            toast("关于")
//        }
//        tv_updatePwd.onClick {
//            startActivity<ResetPwdActivity>()
//        }
//
//        //退出登录，清空本地用户数据
//        btn_logout.setOnClickListener {
//            val user = BaseApplication.mUser
//            mPresent.logout(user.token.toString())
//        }
    }

    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(mActivityComponent).userModule(UserModule()).build().inject(this)
        mPresenter.mView = this
    }

    override fun onGetLogoutResult(result: Boolean) {
        var connection: AndroidDatabaseConnection? = null
        var startPoint: Savepoint? = null
        try {
            val pscOpenHelper = PSCOpenHelper(this)
            val userDao = pscOpenHelper.getDao(UserInfo::class.java)
//            var user = userDao.queryBuilder().queryForFirst()
            val userList: List<UserInfo> = userDao.queryForAll()
            var user : UserInfo? = null
            if (userList.size > 0) {
                user = userList.last()
            }
            connection =
                AndroidDatabaseConnection(pscOpenHelper.writableDatabase, true)
            startPoint = connection.setSavePoint("startLogout")
            connection.isAutoCommit = false // 取消自动提交
            user!!.loginId = -1
            userDao.update(user)
            connection.commit(startPoint) // 提交事务
            Log.e("login","事务正常")
        } catch (e:Exception) {
            Log.e("login","出现ormlite事务异常"+e.localizedMessage)
            if (connection != null) {
                connection.rollback(startPoint)
            }
        }

        startActivity<LoginActivity>()
        finish()
    }

    override fun onError(text: String) {
        super.onError(text)
        startActivity<LoginActivity>()
        finish()
    }

    override fun onClick(view: View?) {
        when(view!!.id){
            R.id.tv_updatePwd -> {
                startActivity<ResetPwdActivity>()
            }

            R.id.btn_logout -> { //退出登录
                val user = BaseApplication.mUser
                mPresenter.logout(user.token.toString())
            }

        }
    }

//    fun onLoginSuccess() {
//
//        val pscOpenHelper = PSCOpenHelper(this)
//        val userDao = pscOpenHelper.getDao(UserInfo::class.java)
//        var user = userDao.queryBuilder().queryForFirst()
//        user.id = -1
//
////        val pscOpenHelper = this.let { it1 -> PSCOpenHelper(it1) }
////        val userDao = pscOpenHelper!!.getDao(UserInfo::class.java)
////        userDao.delete(UserInfo())
//        this!!.runOnUiThread {
//            startActivity<LoginActivity>()
//            finish()
//        }
//        toast("退出登录成功")
//    }
//
//    fun onLoginFailed() {
//        this!!.runOnUiThread {
//            startActivity<LoginActivity>()
//            finish()
//        }
////        toast("退出登录失败")
//    }


}
